package com.herui.iot.config;


import com.herui.sdyu_lib.http.HttpUtils;
import com.herui.sdyu_lib.http.okhttp.RetrofitCall;
import com.herui.sdyu_lib.utils.LogUtils;
import com.herui.sdyu_lib.utils.StringUtils;
import com.herui.sport_care.data.RepositoryFactory;
import com.herui.sport_care.data.user.source.UserSource;

import java.io.IOException;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by amanda.shan on 2019/9/25.
 */
public class ServerConfig {

//    public static String serverIP = "118.31.1.101";
//    public static int port = 7096;

    public static String serverIP = "https://ssta-pre-ser.protonhealth.cn/";    // 50.144.42.119
    // 192.168.50.37
    public static int port = 6969;

    public static int pushPort = 7094;
    public static String successCode = "0000";

    public static String prefx = "/watch/api/v2";    //"/watch/api/v2";

    public static String getMlnxUrlRoot() {
        return "https://ssta-pre-ser.protonhealth.cn";
//        return "http://" + serverIP + ":" + port+"/watch/api/v2/";
    }

    public static String getWebsocketUrl(){
        return "wss://ssta-pre-ser.protonhealth.cn/agreement/assess/websocket/reviewer/";
    }

    public static RetrofitCall retrofitCall =
            HttpUtils.Retrofit().baseUrl(getMlnxUrlRoot()).setInterceptor(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            HttpUrl url = request.url();

            UserSource userSource = RepositoryFactory.getInstance().getUserSource();
            String token = userSource.getToken();
//            token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJwYXNzd29yZCI6IjEyMyIsImlzcyI6Imh0dHA6Ly93d3cucHJvdG9udGVrLmNvbS8iLCJpZCI6MjYsImV4cCI6MTY4NjAxNjMzOSwiaWF0IjoxNjcwNDY0MzM5LCJqdGkiOiJhZmNiZTAyMi05ZTU3LTRhYmQtOTcyOS02NmI0MDEwNmMzNDcifQ.QoajMJ2Qjkjy5j_WKZilEnv-KgHiYg0SAwFVcpLmSZg";
            if (!StringUtils.isEmpty(token)) {
                request =
                        request.newBuilder().header("token", token).header("source", "APP").build();
            }
            Response response = chain.proceed(request);
            LogUtils.i("response:" + url.toString() + " code:" + response.code());
            return response;
        }
    }).buildRetrofit();

    public static void init(String ip, int port) {
//        ServerConfig.serverIP = ip;
//        ServerConfig.port = port;
//
//        StorageUtils.saveSharedPreference("serverIp", serverIP);
//        StorageUtils.saveSharedPreference("port", port);

    }

}
